import $ from 'jQuery';
let overAllIds = new Array();  //列表选中的全局数组
(function () {
    let storeParams = JSON.parse(IOT.getSessionStore(URI.OPERATE.GROUPS.UPDATE.PAGE));
    let $vue = new Vue({
        el: '.groups-update-hook',
        data: {
            account: {
                "cTime": "",
                "cUser": "",
                "groupCode": "",
                "groupName": "",
                "groupNote": "",
                "proId": "",
                "userId": "",
                "groupState": "",
                "proName": "",
                "userName": ""
            }
        },
        methods: {
            goBack: function () {
                M.Page.emitPrePage();
            },
            lightPlan:function () {/*照明计划页面*/
                M.Page.emit(URI.OPERATE.GROUPS.DETAIL.PLAN);
            },
            createBtn:function () {/*新建分组*/
                M.Page.emit(URI.OPERATE.GROUPS.CREATE.PAGE);
            },
            transfersBtn:function () {/*转移分组按钮*/
                let operateClear = overAllIds;
                if(operateClear.length == 0){
                    layer.alert('请选择一条数据');
                    return false;
                }else {
                    layer.confirm('&nbsp;&nbsp; &nbsp;&nbsp;你确定要转移分组吗？', {
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        let params = {};
                        /*params.logId = operateClear.map(function (item,index,input) {
                            return item.logId;
                        });*/
                        IOT.showOverlay('清理中，请稍等...');
                        IOT.getServerData(URI.GROUPS.UPDATE.GROUPS_CONFIRM, params, (ret) => {
                            IOT.hideOverlay();
                            if (ret && ret.code === 200) {
                                IOT.tips('转移分组成功！', 'success');
                                layer.closeAll();
                                M.Table.refresh.all();
                            } else {
                                IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                            }
                        });
                    }, function(){
                        //取消
                    });
                }
            }
        },
        created: function () {
            this.account = storeParams;
            //获取转移分组的下拉选项
            /*IOT.getServerData(URI.OPERATE.GROUPS.DETAIL.DATA, {userId: storeParams.userId}, (ret) => {//获取详情
                if (ret.code === 200) {
                    this.account = $.extend({}, this.account, ret.data);
                } else {
                    IOT.tips(ret.message || '服务器请求失败，稍后再试！', 'error');
                }
            });*/
        },
        mounted: function () {
            this.$nextTick(() => {
                let tableHookName = $('.groupsDetail-table-hook');
                var $tableHook =tableHookName.bootstrapTable({
                    scrollbar: 'groupsDetail-table-body',
                    striped: true, // 设置为 true 会有隔行变色效果
                    pagination: true, // true 显示分页
                    paginationDetail: true, // 分页详情
                    sidePagination: 'server', // 服务器端
                    method: 'post',
                    url: URI.OPERATE.GROUPS.DETAIL.LIST,
                    debug: false,
                    cache: false,
                    pageNumber: 1,
                    pageSize: 20,
                    pageList: [10, 20, 50, 100],
                    // fixedColumns: true,
                    customButton: '',
                    customQueryParams: function (params) {
                        params.groupCode = storeParams.groupCode;
                        return params;
                    },
                    onLoadSuccess: function (data) {
                        //console.log(JSON.stringify(data));
                    },
                    onLoadError: function (status, xhr) {
                    },
                    columns: [
                        {
                            field: 'checkStatus', checkbox: true, align: 'center', fixedLeft: true, width: 60,
                            formatter: function (i,row) {//每次加载checkbox时判断当前row的groupCode是否已经存在全局里
                                for(var k=0;k<overAllIds.length;k++){
                                    //全局变量中含有id，则该复选框选中
                                    if(overAllIds[k].groupCode == row.groupCode){
                                        return {
                                            checked : true //存在则选中
                                        }
                                    }
                                }
                            }
                        },
                        {
                            field: 'lightCode', title: '单灯编号', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'lightName', title: '单灯名称', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'onenetCode', title: '终端编号', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'imei', title: 'IMEI号', align: 'center', fixedLeft: true,tips:true
                        },
                        {
                            field: 'imsi', title: 'IMSI号', align: 'center',fixedLeft: true,tips:true
                        },
                        {
                            field: 'states', title: '状态', align: 'center',fixedLeft: true,tips:true,
                            formatter: function (val, rowData, index) {
                                if(val == 0){
                                    val = '开灯';
                                }else if(val == 1){
                                    val = '关灯';
                                }else if(val == 2){
                                    val = '故障';
                                }else if(val == 3){
                                    val = '离线';
                                }else{
                                    val = '其他';
                                }
                                return val;
                            }
                        },
                        {
                            field: 'installTime', title: '安装时间', align: 'center', fixedLeft: true,tips:true,
                            formatter: function (val, row, index) {
                                return val;
                            }
                        }
                    ]
                });
                //表格结束
                $('.operateLogs-table-hook').on('uncheck.bs.table check.bs.table check-all.bs.table uncheck-all.bs.table',function(e,rows){
                    var data = $.isArray(rows) ? rows : [rows]; //点击时获取选中的行或取消选中的行
                    examine(e.type,data); //保存到全局里
                });
                
                $('.update-form-hook').validate({
                    debug: true,
                    rules: {
                        groupName: {
                            required: true,
                            minlength: 1,
                            maxlength: 50
                        },
                        userName: {
                            required: true,
                            minlength: 1,
                            maxlength: 20
                        },
                        groupNote:{
                            maxlength: 500
                        }
                    },
                    messages: {
                        groupName: {
                            required: '请输入分组名称！'
                        },
                        userName: {
                            required: '请输入维护人！'
                        }
                    },
                    submitHandler: function (form) {
                        /*if($.trim($vue.$data.account.userName) == ""){
                            layer.alert('用户姓名不能为空');
                            return false;
                        }
                        var reg4 = /^[a-zA-Z\u4e00-\u9fa5.]+$/;
                        if(reg4.test($.trim($vue.$data.account.userName)) == false){
                            layer.alert('用户名格式不正确');
                            return false;
                        }*/
                        IOT.showOverlay('保存中，请稍等...');
                        let params = $vue.$data.account;
                        IOT.getServerData(URI.OPERATE.GROUPS.UPDATE.SAVE, params, function (ret) {
                            IOT.hideOverlay();
                            if (ret && ret.code === 200) {
                                IOT.tips('修改成功！', 'success');
                                M.Table.refresh.all();
                                M.Page.emitPrePage();
                            } else {
                                IOT.tips(ret.msg || '操作失败，请重试！', 'error', 1000, null);
                            }
                        });
                        return false;
                    }
                });
            });
        }
    });
})();

function examine(type,data){//操作选中的行
    if(type.indexOf('uncheck')==-1){
        $.each(data,function(i,v){//如果是选中则添加选中行的数据
            //添加时，判断一行或多行的groupCode是否已经在数组里，不存则添加　
            overAllIds.indexOf(v.groupCode) == -1 ? overAllIds.push(v) : -1;
        });
    }else{
        $.each(data,function(i,v){
            for(var k = overAllIds.length-1;k>=0;k--){
                if(overAllIds[k].groupCode == v.groupCode)
                    overAllIds.splice(k,1);//删除取消选中行
            }
        });
    }
}